const MyPromise = require('./MyPromise')

// console.log(MyPromise)
// console.log(new MyPromise(1))
// console.log(new MyPromise('#i am no a function#'))
let promise = new MyPromise((resolve, reject) => {
    // 这种异步情况不是MyPromise方式的异步识别不出来。
    setTimeout(()=>{ 
        resolve('成功')
    }, 2000)
    // reject('失败')
    // throw new Error('executor error!!!')
    // resolve('成功')
})
/* function p2() {
    return new MyPromise((resolve, reject) => {
        resolve('inner MyPromise')
    })
}
promise.then(value => {
    console.log(value)
    return p2()
}, reason => {
    return reason
}).then(value => {
    console.log(value)
}) */
// 自调用异常检测情况测试
/* var p1 =  */
/* promise.then(value => {
    console.log(value)
    // throw new Error('then error!!!')
    // return p1;
}, reason => {
    console.log(reason)
    return 999
}).then(value => {
    console.log(value)
}, reason => {
    console.log(reason)
}) */

// 成功情况测试
/* promise.then((value) => {
    console.log('data-1', value)
}, console.log)

promise.then((value) => {
    setTimeout(() => {
        console.log('data-2异步', value)

    }, 3000)
    console.log('data-2-1', value)

}, console.log)

promise.then((value) => {
    console.log('data-3', value)
}, console.log) */

// promise.then(4,'b').then().then(value => {
//     console.log('value return from the first resolve')
//     console.log(value)
// },reason => {
//     console.log(reason)
// })

// let p1 = MyPromise.resolve('100'), p2 = MyPromise.resolve(6)
// MyPromise.all(['a', 1, p1, p2]).then(value => {
//     console.log(value)
//     return value.length
// }, reason => {
//     console.log(reason)
// }).finally(() => {
//     console.log('finally happen!')
//     throw new Error('i get some mistake!')
//     return promise.then(value=> console.log(value))
// }).then(value => {
//     console.log(value)
// }, reason => {
//     console.log(reason)
// })
// .catch(console.warn)
// console.log(Promise)
// new Promise的时候的executor必须时一个function
let originPromise = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve('good...')
    }, 2000)
    // reject('bad...')
})

// // then可能再同一个promise实例上多次调用，这就会产生一个回调队列
// // 成功情况测试
// originPromise.then('a', 'b'/* (value) => {
//     console.log('data-1', value)
// }, console.log */).then(console.log)

// originPromise.then((value) => {
//     setTimeout(() => {
//         console.log('data-2异步', value)

//     }, 3000)
//     console.log('data-2-1', value)

// }, console.log)

// originPromise.then((value) => {
//     console.log('data-3', value)
// }, console.log)

// originPromise.then('',console.log)
Promise.race([originPromise,2222,300]).then(value=> {
    console.log(value)
})
MyPromise.race([promise,2222,300]).then(value=> {
    console.log(value)
})